home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / vol_300 / 395_01 / avl / example.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-09-04  |  2.5 KB  |  120 lines

  1. /* implementation of a simple "environment".  identifiers are
  2.    associated with arbitrary strings */
  3.  
  4. #include <string.h>
  5. #include <stdio.h>
  6.  
  7. #include "sortlist.h"
  8.  
  9. typedef struct
  10.   {
  11.     char *id;
  12.     char *value;
  13.   }
  14. ID_WITH_VALUE;
  15.  
  16. int key_elem_compare(const void *id, const void *id_with_value)
  17.   {
  18.     return(stricmp((char *) id, ((ID_WITH_VALUE *) id_with_value)->id));
  19.   }
  20.  
  21. int elem_elem_compare(const void *id_with_value_1,
  22.                       const void *id_with_value_2)
  23.   {
  24.     return(stricmp(((ID_WITH_VALUE *) id_with_value_1)->id,
  25.                    ((ID_WITH_VALUE *) id_with_value_2)->id));
  26.   }
  27.  
  28. typedef SORT_LIST ENV;
  29.  
  30. /* initialize environment to empty state */
  31. void init_env
  32.   (
  33.     ENV *e
  34.   )
  35.   {
  36.     init_sort_list(e, sizeof(ID_WITH_VALUE),
  37.                elem_elem_compare, key_elem_compare);
  38.  
  39.     return;
  40.   }
  41.  
  42. /* add id - value pair to environment */
  43. int add_env(ENV *e, char *id, char *value)
  44.   {
  45.     ID_WITH_VALUE iv;
  46.  
  47.     iv.id = malloc(strlen(id) + 1);
  48.     if (!iv.id)
  49.       return(-1);
  50.     iv.value = malloc(strlen(value) + 1);
  51.     if (!iv.value)
  52.       return(-1);
  53.     strcpy(iv.id, id);
  54.     strcpy(iv.value, value);
  55.  
  56.     if (add_sort_list(e, (void *) &iv) != SL_SUCCESS)
  57.       return(-1);
  58.     return(0);
  59.   }
  60.  
  61. /* find value that goes with id */
  62. char *find_env(ENV *e, char *id)
  63.   {
  64.     ID_WITH_VALUE *iv =
  65.       (ID_WITH_VALUE *) find_sort_list(e, MATCH_EQUAL, id);
  66.  
  67.     if (!iv)
  68.       return((char *) 0);
  69.  
  70.     return(iv->value);
  71.   }
  72.  
  73. /* remove id - value pair from environment */
  74. void del_env(ENV *e, char *id)
  75.   {
  76.     ID_WITH_VALUE iv;
  77.  
  78.     if (delete_sort_list(e, id, (void *) &iv) == SL_SUCCESS)
  79.       {
  80.     free(iv.id);
  81.     free(iv.value);
  82.       }
  83.  
  84.     return;
  85.   }
  86.  
  87. static int prn_apply(void *id_with_value, void *dummy)
  88.   {
  89.     printf("%s=%s\n", ((ID_WITH_VALUE *) id_with_value)->id,
  90.                       ((ID_WITH_VALUE *) id_with_value)->value);
  91.  
  92.     return(0);
  93.   }
  94.  
  95. /* print environment in ascending order */
  96. void print_env(ENV *e)
  97.   {
  98.     apply_sort_list(e, prn_apply, (void *) 0, 1, (void *) 0);
  99.  
  100.     return;
  101.   }
  102.  
  103. static int clear_apply(void *id_with_value, void *dummy)
  104.   {
  105.     free(((ID_WITH_VALUE *) id_with_value)->id);
  106.     free(((ID_WITH_VALUE *) id_with_value)->value);
  107.  
  108.     return(0);
  109.   }
  110.  
  111. /* delete everything in environment */
  112. void clear_env(ENV *e)
  113.   {
  114.     apply_sort_list(e, clear_apply, (void *) 0, 1, (void *) 0);
  115.  
  116.     clear_sort_list(e);
  117.  
  118.     return;
  119.   }
  120.